import { CatchTraceStatus } from './base/ProfilerConstant'
import util from '@ohos.util'
import Brightness from '@system.brightness'
import prompt from '@system.prompt';
import SPLogger from '../utils/SPLogger';

export default class WorkerHandler {
    static socketHandler(result) {
        let arr: Array<String> = []
        arr = result.data.split("$")
        switch (arr[0]) {
            case "RAM":
                let recv=result.data as String
                globalThis.ramArr.push(recv.substring(4))
                break;
            case "FPS":
                globalThis.fpsArr.push(arr[1])
                globalThis.fpsJitterArr.push(arr[2])
                globalThis.timerFps = arr[1]
                if (
                globalThis.catchTraceState == CatchTraceStatus.catch_trace_start ||
                globalThis.catchTraceState == CatchTraceStatus.catch_trace_finish ||
                globalThis.catchTraceState == CatchTraceStatus.catch_trace_first_running) {
                    if (globalThis.fpsJitterArr != undefined && globalThis.fpsJitterArr != null && globalThis.fpsJitterArr != "") {
                        let tempQueue: Array<String> = globalThis.fpsJitterArr
                        let curJitter = tempQueue.pop()
                        let tempJitterArr = curJitter.split("==")
                        for (var i = 0; i < tempJitterArr.length; i++) {
                            let tmp = tempJitterArr[i]
                            let jitter = parseInt(tmp) / 1e6
                            if (jitter > 100) {
                                globalThis.jitterTrace = true
                                return
                            }
                        }
                    }
                }
                break;
            case "UdpStatus":
                let isSocketConnect = Number(arr[1]).valueOf()
                if (isSocketConnect > 0) {
                    globalThis.useDaemon = true
                } else {
                    globalThis.useDaemon = false
                }
                break
            case 'GPUCounter':
                let recvData=(result.data as String).substring(11)
                globalThis.gpuCounterArr.push(recvData)
                SPLogger.DEBUG('WorkerHandler','gpu_counter: socket recv:' + recvData)
                if (globalThis.recvGPUCounter) {
                    globalThis.recvGPUCounter()
                    globalThis.recvGPUCounter=null
                }

                break
            case 'TRAFFIC':
                let trafficData=(result.data as String).substring(8)
                let sp = trafficData.split('$$')
                for (let i = 0; i < sp.length; i++) {
                    const element = sp[i];
                    let subEles=element.split('||')
                    let eleName = subEles[0]
                    if (eleName == 'networkUp') {
                        globalThis.networkUp = subEles[1]
                    }else if (eleName == 'networkDown'){
                        globalThis.networkDown = subEles[1]
                    }
                }
                if (globalThis.recvTraffic) {
                    globalThis.recvTraffic()
                }
                break
            case 'Error':
                let subEles=(result.data as String).split('||')
                if (subEles[1]) {
                    // prompt.showToast({ message: subEles[1], duration: 1000 })
                }
                break
            default:
                break;
        }
    }
}